package org.ligson.fwc.core.controller;


import org.ligson.fwc.api.vo.WebResult;
import org.ligson.fwc.core.entity.BasicEntity;
import org.ligson.fwc.core.entity.ErrorField;
import org.ligson.fwc.core.entity.Pagination;
import org.ligson.fwc.core.exception.EntityValidException;
import org.ligson.fwc.core.service.BaseService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.Serializable;
import java.util.List;

/**
 * Created by ligson on 2017/4/20.
 */
public abstract class CrudController<E extends BasicEntity, PK extends Serializable> extends BaseController {

    public abstract BaseService<E, PK> getService();

    @PostMapping("/add")
    @ResponseBody
    public WebResult add(@RequestBody E e) {
        webResult.clear();
        try {
            getService().add(e);
            webResult.setSuccess(true);
            webResult.put("entity", e);
        } catch (EntityValidException ex) {
            transferEx(ex, webResult);
        }
        return webResult;
    }

    @PostMapping("/query")
    @ResponseBody
    public Pagination<E> query(@RequestBody E e) {
        return getService().findAllBy(e);
    }

    @PostMapping("/{pk}")
    @ResponseBody
    public WebResult get(@PathVariable PK pk) {
        webResult.clear();
        E e = getService().get(pk);
        if (e != null) {
            webResult.setSuccess(true);
            webResult.put("entity", e);
        } else {
            webResult.setSuccess(false);
            webResult.setErrorMsg("根据pk:" + pk + "未找到对象");
        }
        return webResult;
    }

    @PostMapping("/delete")
    @ResponseBody
    public WebResult delete(@RequestBody E e) {
        webResult.clear();
        getService().delete(e);
        webResult.setSuccess(getService().findBy(e) == null);
        if (!webResult.isSuccess()) {
            webResult.setErrorMsg("关联数据未清理");
        }
        return webResult;
    }

    @PostMapping("/delete/{pk}")
    @ResponseBody
    public WebResult deleteByPrimaryKey(@PathVariable PK pk) {
        webResult.clear();
        getService().deleteByPrimaryKey(pk);
        webResult.setSuccess(true);
        if (!webResult.isSuccess()) {
            webResult.setErrorMsg("关联数据未清理");
        }
        return webResult;
    }

    protected void transferEx(EntityValidException ex, WebResult webResult) {
        webResult.setSuccess(false);
        List<ErrorField> errorFields = ex.getEntity().getErrorFields();
        StringBuilder builder = new StringBuilder("数据库异常(");
        int i = 0;
        for (ErrorField errorField : errorFields) {
            builder.append(i + 1).append(".").append("属性:").append(errorField.getField().getName()).append(errorField.getErrorMsg()).append(";");
        }
        builder.append(")");
        logger.error(builder.toString(), ex);
        webResult.setErrorMsg(builder.toString());
        webResult.put("ex", ex);
    }

    @PostMapping("/update")
    @ResponseBody
    public WebResult update(@RequestBody E e) {
        webResult.clear();
        try {
            getService().update(e);
            webResult.setSuccess(true);
            webResult.put("entity", e);
        } catch (EntityValidException ex) {
            transferEx(ex, webResult);
        }
        return webResult;
    }

    @PostMapping("/updateByPrimaryKey")
    @ResponseBody
    public WebResult updateByPrimaryKey(@RequestBody E e) {
        webResult.clear();
        try {
            getService().updateByPrimaryKey(e);
            webResult.setSuccess(true);
            webResult.put("entity", e);
        } catch (EntityValidException ex) {
            transferEx(ex, webResult);
        }
        return webResult;
    }

}
